package com.nf.health.app.inquiry.fragment;


import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.nf.health.app.R;
import com.nf.health.app.customview.ClearEditText;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.fragment.IBaseFragment;
import com.nf.health.app.inquiry.activity.FastInquiryActivity;
import com.nf.health.app.inquiry.activity.InquiryDetailActivity;
import com.nf.health.app.inquiry.adapter.InquiryNoCompleteAdapter;
import com.nf.health.app.inquiry.bean.InquiryBean;
import com.nf.health.app.utils.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A
 * 提问fragment未完成
 */
public class InquiryNoCompleteFragment extends IBaseFragment implements View.OnClickListener {

    private static final String GET_INQUIRY_LIST = "get_inquiry_list";
    @Bind(R.id.et_search)
    ClearEditText etSearch;
    @Bind(R.id.tw_consul_listview)
    DragListView twConsulListview;
    @Bind(R.id.tv_go_inquiry)
    TextView tvGoInquiry;
    @Bind(R.id.ll_no_inquiry)
    LinearLayout llNoInquiry;

    private List<InquiryBean> mListBean = new ArrayList<InquiryBean>();

    private int page = 1;
    private int pageSize = 20;

    private InquiryNoCompleteAdapter mAdapter;
    private boolean isLoadMore = false;//记录加载更多
    private String keyWords = "";

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        addViewFillInRoot(R.layout.fragment_tiwen);
        ButterKnife.bind(this, rootView);
        initListener();
    }

    private void initListener() {
        tvGoInquiry.setOnClickListener(this);
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (etSearch.isHasFoucs()) {
                    etSearch.setClearIconVisible(s.length() > 0);
                }
                keyWords = s.toString();
                //每次搜索内容改变,清空集合
                page = 1;
                mListBean.clear();
                initData(GET_INQUIRY_LIST);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        twConsulListview.setRefreshableAndLoadMoreable(true, true);
        twConsulListview.setOnRefreshAndLoadMoreListener(new DragListView.onRefreshAndLoadMoreListener() {

            @Override
            public void onRefresh() {
                isLoadMore = false;
                page = 1;
                initData(GET_INQUIRY_LIST);
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                page++;
                initData(GET_INQUIRY_LIST);
            }
        });
        twConsulListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString(InquiryDetailActivity.INQUIRY_ID, mListBean.get(position - 1).getInquiryid());
                bundle.putBoolean(InquiryDetailActivity.IS_COMPLET, false);
                ActivityUtils.showActivity(getActivity(), InquiryDetailActivity.class, bundle);
            }
        });

    }

    private void initData(String flag) {
        provider.getInquiryList(page + "", pageSize + "", "0", keyWords, flag);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void handleActionSuccess(String actionName, Object object) {
        twConsulListview.completeLoadMore();
        twConsulListview.completeRefresh();
        if (GET_INQUIRY_LIST.equals(actionName)) {
            List<InquiryBean> newListBean = (List<InquiryBean>) object;
            if (newListBean.size() == 0) {
               /* showToast("没有更多数据");*/
                //一条数据都没有
                if (mListBean.size() == 0) {
                    twConsulListview.setVisibility(View.GONE);
                    llNoInquiry.setVisibility(View.VISIBLE);
                }
                return;
            }
            if (!isLoadMore) {
                mListBean.clear();
            }
            mListBean.addAll(newListBean);
            //隐藏无数据提示
            twConsulListview.setVisibility(View.VISIBLE);
            llNoInquiry.setVisibility(View.GONE);
            if (mAdapter == null) {
                mAdapter = new InquiryNoCompleteAdapter(getActivity(), mListBean);
                twConsulListview.setAdapter(mAdapter);
            }
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void handleActionError(String actionName, String response, VolleyError error) {
        super.handleActionError(actionName, response, error);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_go_inquiry:
                ActivityUtils.skipActivity(getActivity(), FastInquiryActivity.class);
                break;
            default:
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        isLoadMore = false;
        page = 1;
        initData(GET_INQUIRY_LIST);
    }

}